package com.yangsir.mysite.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yangsir.mysite.Dto.ArticleUserDto;
import com.yangsir.mysite.pojo.Article;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author yf
 * @description 针对表【article(文章)】的数据库操作Mapper
 * @createDate 2024-06-22 16:55:16
 * @Entity generator.pojo.Article
 */
public interface ArticleMapper extends BaseMapper<Article> {

    // 获取所有文章(管理员权限)
    @Select("SELECT article.id, article.title, article.img_url, article.type, article.category_id, " +
            "article.is_top, article.hot, article.comment_num, article.like_num, " +
            "user.user_account, article.create_time, user.user_role " +
            "FROM article INNER JOIN user ON user.id = article.user_id " +
            "WHERE article.is_delete != 1 " +
            "ORDER BY article.is_top DESC, article.create_time DESC")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "title", property = "title"),
            @Result(column = "img_url", property = "imgUrl"),
            @Result(column = "type", property = "type"),
            @Result(column = "category_id", property = "categoryId"),
            @Result(column = "is_top", property = "isTop"),
            @Result(column = "hot", property = "hot"),
            @Result(column = "comment_num", property = "commentNum"),
            @Result(column = "like_num", property = "likeNum"),
            @Result(column = "user_account", property = "userAccount"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "user_role", property = "userRole")
    })
    List<ArticleUserDto> selectAllArticleWithUser();

    // 获取用户自己的文章(作者)
    @Select("SELECT article.id, article.title, article.img_url, article.type, article.category_id, " +
            "article.is_top, article.hot, article.comment_num, article.like_num, " +
            "user.user_account, article.create_time, user.user_role " +
            "FROM article INNER JOIN user ON user.id = article.user_id " +
            "WHERE user.id=#{id} AND article.is_delete != 1 " +
            "ORDER BY article.is_top DESC, article.create_time DESC")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "title", property = "title"),
            @Result(column = "img_url", property = "imgUrl"),
            @Result(column = "type", property = "type"),
            @Result(column = "category_id", property = "categoryId"),
            @Result(column = "is_top", property = "isTop"),
            @Result(column = "hot", property = "hot"),
            @Result(column = "comment_num", property = "commentNum"),
            @Result(column = "like_num", property = "likeNum"),
            @Result(column = "user_account", property = "userAccount"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "user_role", property = "userRole")
    })
    List<ArticleUserDto> selectArticleWithUser(Long id);


    @Select("SELECT article.id, article.title, article.img_url, article.type, article.category_id, article.is_top, " +
            "article.hot, article.comment_num, article.like_num, " +
            "user.user_account, article.create_time, user.user_role " +
            "FROM article INNER JOIN user ON user.id = article.user_id " +
            "WHERE article.type = #{param1} AND article.user_id = #{param2} " +
            "ORDER BY article.is_top DESC, article.create_time DESC")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "title", property = "title"),
            @Result(column = "img_url", property = "imgUrl"),
            @Result(column = "type", property = "type"),
            @Result(column = "category_id", property = "categoryId"),
            @Result(column = "is_top", property = "isTop"),
            @Result(column = "hot", property = "hot"),
            @Result(column = "comment_num", property = "commentNum"),
            @Result(column = "like_num", property = "likeNum"),
            @Result(column = "user_account", property = "userAccount"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "user_role", property = "userRole")
    })
    List<ArticleUserDto> selectArticleWithUserByType(@Param("type") Integer type, @Param("id") Long id);

    @Select("SELECT article.id, article.title, article.img_url, article.type, article.category_id, article.is_top, " +
            "article.hot, article.comment_num, article.like_num, " +
            "user.user_account, article.create_time, user.user_role " +
            "FROM article INNER JOIN user ON user.id = article.user_id " +
            "WHERE article.type = #{type} " +
            "ORDER BY article.is_top DESC, article.create_time DESC")
    List<ArticleUserDto> selectAllArticleWithUserByType(Integer type);

    @Select("SELECT article.id, article.title, article.img_url, article.type, article.category_id, article.is_top, " +
            "article.hot, article.comment_num, article.like_num, " +
            "user.user_account, article.create_time, user.user_role " +
            "FROM article INNER JOIN user ON user.id = article.user_id " +
            "WHERE article.category_id = #{param1} AND article.user_id = #{param2} " +
            "ORDER BY article.is_top DESC, article.create_time DESC")
    List<ArticleUserDto> selectArticleWithUserByCategory(@Param("categoryId") Long categoryId, @Param("id") Long id);

    @Select("SELECT article.id, article.title, article.img_url, article.type, article.category_id, article.is_top, " +
            "article.hot, article.comment_num, article.like_num, " +
            "user.user_account, article.create_time, user.user_role " +
            "FROM article INNER JOIN user ON user.id = article.user_id " +
            "WHERE article.category_id = #{categoryId} " +
            "ORDER BY article.is_top DESC, article.create_time DESC")
    List<ArticleUserDto> selectAllArticleWithUserByCategory(Long categoryId);

    // 模糊查询
    @Select("SELECT article.id, article.title, article.img_url, article.type, article.category_id, article.is_top, " +
            "article.hot, article.comment_num, article.like_num, " +
            "user.user_account, article.create_time, user.user_role " +
            "FROM article INNER JOIN user ON user.id = article.user_id " +
            "WHERE (article.title like CONCAT('%', #{str}, '%') OR " +
            "user.user_account like CONCAT('%', #{str}, '%')) AND article.is_delete != 1 " +
            "ORDER BY article.is_top DESC, article.create_time DESC")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "title", property = "title"),
            @Result(column = "img_url", property = "imgUrl"),
            @Result(column = "type", property = "type"),
            @Result(column = "category_id", property = "categoryId"),
            @Result(column = "is_top", property = "isTop"),
            @Result(column = "hot", property = "hot"),
            @Result(column = "comment_num", property = "commentNum"),
            @Result(column = "like_num", property = "likeNum"),
            @Result(column = "user_account", property = "userAccount"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "user_role", property = "userRole")
    })
    List<ArticleUserDto> selectArticleWithUserLikeStr(String str);

    // 随机获取5条数据
    @Select("SELECT * FROM article ORDER BY RAND() LIMIT #{num}")
    List<Article> randomArticles(Integer num);

}




